using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class manager : MonoBehaviour
{
    public GameObject Enemy01;
    //声明公共Enemy01游戏对象变量
    public GameObject Enemy02;
    //声明公共Enemy02游戏对象变量
    public GameObject Enemy03;
    //声明公共Enemy03游戏对象变量
    public GameObject Boss;
    public List<GameObject> Enemy_list;
    public List<List<double>> levelDesign = new List<List<double>>();
    private int enemyNumber;
    private Text currEnemyNumber;
    private Text countEnemyNumber;
    //关卡等级
    public int level = 1;
    //声明公共Enemy_list游戏对象列表
    private GameObject intergralManageObj;
    private GameObject next;
    public bool isEnd = false;
    void Start()
    //Start()方法在第一帧前调用
    {
        //初始化难度
        List<double> enemyNumber = new List<double> { 30, 40, 50, 60, 70 };
        List<double> enemyHp = new List<double> { 1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7 };
        levelDesign.Add(enemyNumber);
        levelDesign.Add(enemyHp);
        addEnemyList();
        //向Enemy_list列表添加Enemy03
        //3秒后调用Enemy_maker方法,并每1秒调用一次
        StartSpawing();
        intergralManageObj = GameObject.Find("intergral");
    }
    // Update is called once per frame  
    void Update()
    //Update()方法每一帧调用一次
    {
        if (enemyNumber >= levelDesign[0][level - 1])
        {
            GameObject enemy_now = Instantiate(Boss);
            //随机颜色
            enemy_now.gameObject.GetComponent<Renderer>().material.color = Random.ColorHSV();
            //根据i的值,实例化Enemy_list列表中的游戏对象,赋值给enemy_now
            enemy_now.transform.position = new Vector3(gameObject.transform.position.x, enemy_now.transform.position.y, gameObject.transform.position.z);
            StopSpawning();  //停止生成敌人
            enemyNumber = 0;
        }
        if (isEnd && next == null && level!=5)
        {
            next = Instantiate((GameObject)Resources.Load("next"));
            GameObject nextPos = GameObject.Find("nextEmpty");
            next.transform.position = nextPos.transform.position;
        }
    }
    void Enemy_maker()
    {
        //Enemy_maker方法
        int i = Random.Range(0, 3);
        //随机生成0-2之间的整数,赋值给i 
        GameObject enemy_now = Instantiate(Enemy_list[i]);
        //随机颜色
        enemy_now.gameObject.GetComponent<Renderer>().material.color = Random.ColorHSV();
        //根据i的值,实例化Enemy_list列表中的游戏对象,赋值给enemy_now
        enemy_now.transform.position = new Vector3(gameObject.transform.position.x, enemy_now.transform.position.y, gameObject.transform.position.z);
        //设置enemy_now的位置为gameObject的位置
        enemyNumber += 1;
        currEnemyNumber = intergralManageObj.transform.Find("currEnemy").GetComponent<Text>();
        //获取interGral值
        currEnemyNumber.text = enemyNumber.ToString();
        countEnemyNumber = intergralManageObj.transform.Find("countEnemy").GetComponent<Text>();
        //获取interGral值
        countEnemyNumber.text = levelDesign[0][level - 1].ToString();
        Debug.Log("已经生成敌人数量：" + enemyNumber + "总数:" + countEnemyNumber.text);
    }
    void addEnemyList()
    {
        Enemy01 = (GameObject)Resources.Load("EnemyCube");
        //从Resources文件夹加载Enemy01资源,赋值给Enemy01变量
        Enemy02 = (GameObject)Resources.Load("EnemyOval");
        //从Resources文件夹加载Enemy02资源,赋值给Enemy02变量
        Enemy03 = (GameObject)Resources.Load("EnemySphere");
        Boss = (GameObject)Resources.Load("Boss");
        //从Resources文件夹加载Enemy03资源,赋值给Enemy03变量
        Enemy_list.Add(Enemy01);
        //向Enemy_list列表添加Enemy01
        Enemy_list.Add(Enemy02);
        //向Enemy_list列表添加Enemy02
        Enemy_list.Add(Enemy03);

    }
    //开始生成敌人
    public void StartSpawing()
    {
        InvokeRepeating("Enemy_maker", 3f, 1.5f);
    }
    //停止生成敌人
    public void StopSpawning()
    {
        CancelInvoke("Enemy_maker");
    }
}